package io.techery.progresshint.addition.widget;

import com.warkiz.widget.IndicatorSeekBar;
import com.warkiz.widget.OnSeekChangeListener;
import com.warkiz.widget.SeekParams;

import io.techery.progresshint.AttrValueUtils;
import io.techery.progresshint.ProgressHintDelegate;
import io.techery.progresshint.ResourceTable;
import io.techery.progresshint.addition.HorizontalProgressHintDelegate;
import io.techery.progresshint.addition.ViewUtil;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Slider;
import ohos.app.Context;

public class SeekBar extends IndicatorSeekBar implements
        ProgressHintDelegate.SeekBarHintDelegateHolder, OnSeekChangeListener {
    private ProgressHintDelegate hintDelegate;
    private int marginLeft;
    private int marginRight;
    private int marginTop;
    private int marginBottom;
    private boolean popupAlwaysShown;
    private int screenWidth;

    /**
     * SeekBar Instance
     *
     * @param context 上下文
     */
    public SeekBar(Context context) {
        this(context, null);
    }

    /**
     * SeekBar Instance
     *
     * @param context 上下文
     * @param attrSet attrSet
     */
    public SeekBar(Context context, AttrSet attrSet) {
        this(context, attrSet, 0);
    }

    /**
     * SeekBar Instance
     *
     * @param context 上下文
     * @param attrSet attrSet
     * @param styleName styleName
     */
    public SeekBar(Context context, AttrSet attrSet, int styleName) {
        super(context, attrSet, styleName);
        popupAlwaysShown = AttrValueUtils.get(attrSet, "popupAlwaysShown", false);
        marginLeft = getMarginByPercent(attrSet, "layout_marginLeftPercent", "0%");
        marginRight = getMarginByPercent(attrSet, "layout_marginRightPercent", "0%");
        marginTop = getMarginByPercent(attrSet, "layout_marginTopPercent", "0%");
        marginBottom = getMarginByPercent(attrSet, "layout_marginBottomPercent", "0%");

        screenWidth = ViewUtil.getDisplayWidthInPx();
        marginLeft = screenWidth * marginLeft / 100;
        marginRight = screenWidth * marginRight / 100;
        marginTop = ViewUtil.getDisplayHeightInPx() * marginTop / 100;
        marginBottom = ViewUtil.getDisplayHeightInPx() * marginBottom / 100;
        init(attrSet, null);
    }

    private int getMarginByPercent(AttrSet attrSet, String key, String defValue) {
        String percent = AttrValueUtils.get(attrSet, key, defValue);
        percent = percent.substring(0, percent.indexOf('%'));
        return Integer.valueOf(percent);
    }

    private void init(AttrSet attrSet, String styleName) {
        hintDelegate = new HorizontalProgressHintDelegate(new Slider(getContext()), attrSet, styleName);
        setShowIndicatorType(4);
        setPopupAlwaysShown(popupAlwaysShown);
        Component ovalComponent = LayoutScatter.getInstance(getContext()).parse(
                ResourceTable.Layout_progress_hint_popup, null, false);
        setIndicatorContentView(ovalComponent);
        getIndicator().initIndicator();
        setOnSeekChangeListener(this);
    }

    @Override
    public ProgressHintDelegate getHintDelegate() {
        return hintDelegate;
    }

    @Override
    public int getMarginLeftByPercent() {
        return marginLeft;
    }

    @Override
    public int getMarginRightByPercent() {
        return marginRight;
    }

    @Override
    public int getMarginTopByPercent() {
        return marginTop;
    }

    @Override
    public int getMarginBottomByPercent() {
        return marginBottom;
    }

    @Override
    public void onSeeking(SeekParams seekParams) {
    }

    @Override
    public void onStartTrackingTouch(IndicatorSeekBar seekBar) {
        if (!popupAlwaysShown) {
            getIndicatorContentView().setVisibility(Component.VISIBLE);
        }
    }

    @Override
    public void onStopTrackingTouch(IndicatorSeekBar seekBar) {
        if (!popupAlwaysShown) {
            getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                @Override
                public void run() {
                    getIndicatorContentView().setVisibility(Component.INVISIBLE);
                }
            }, 100);
        }
    }
}
